﻿using Managistics.Warehouse.Configuration;
using Managistics.Warehouse.Controllers.Interfaces;
using Managistics.Warehouse.Views.Interfaces;
using Managistics.Warehouse.Factories;
using Managistics.Core.Services;
using System.Collections.Generic;
using Managistics.Model;
using Managistics.Model.ValueObject;
using Managistics.Core;

namespace Managistics.Warehouse.Controllers
{
    public class PurchaseController : IPurchaseController
    {
        /// <summary>
        /// Local instance of the configuration class that we'll end up binding to
        /// </summary>
        public ApplicationConfiguration appConfig = null;
        readonly IViewEnterFromPurchaseView fromPurchaseView;
        readonly IMainView mainView;
        private readonly IPurchaseOrderService purchaseOrderService;

        /// <summary>
        /// PurchaseController Constructor
        /// </summary>
        /// <param name="fromPurchaseView"></param>
        /// <param name="view"></param>
        /// <param name="purchaseOrderService"></param>
        public PurchaseController(IViewEnterFromPurchaseView fromPurchaseView, IMainView view,
            IPurchaseOrderService purchaseOrderService)
        {
            this.fromPurchaseView = fromPurchaseView;
            this.mainView = view;
            this.purchaseOrderService = purchaseOrderService;

            // Simply assign the default config object - it gets loaded via 
            // the default constructor defined in AppConfig.cs
            this.appConfig = GlobalApp.Configuration;
            
            fromPurchaseView.LoadPurchaseOrders += LoadPurchaseOrders;
            fromPurchaseView.LoadPurchaseOrderDetails += LoadPurchaseOrderDetails;
            fromPurchaseView.LoadInputSheets += LoadInputSheets;
            fromPurchaseView.LoadInputSheetDetails += LoadInputSheetDetails;
            fromPurchaseView.SaveInputSheet += SaveInputSheet;
            fromPurchaseView.DeleteInputSheet += DeleteInputSheet;
        }

        /// <summary>
        /// open view
        /// </summary>
        public void Run()
        {
            fromPurchaseView.Open();
        }

        /// <summary>
        /// Stop view
        /// </summary>
        /// <param name="key"></param>
        public void Stop(string key) { }

        /// <summary>
        /// Load PurchaseOrders
        /// </summary>
        /// <param name="p"></param>
        private void LoadPurchaseOrders(PurchaseOrderParam p)
        {
            List<PurchaseOrder> ls = purchaseOrderService.GetPurchaseOrders(p);
            fromPurchaseView.SetPurchaseOrderRecord(ls);
        }

        /// <summary>
        /// Load PurchaseOrderDetails
        /// </summary>
        /// <param name="orderId"></param>
        private void LoadPurchaseOrderDetails(string orderId)
        {
            List<PurchaseOrderDetail> ls = purchaseOrderService.GetPurchaseOrderDetails(orderId);
            fromPurchaseView.SetPurchaseOrderDetailRecord(ls);
        }

        /// <summary>
        /// Load InputSheets
        /// </summary>
        /// <param name="orderId"></param>
        private void LoadInputSheets(string orderId)
        {
            List<InputSheet> ls = purchaseOrderService.GetInputSheetsByOrder(orderId);
            if (ls != null && ls.Count > 0)
            {
                InputSheet emptyInputSheet = new InputSheet();
                emptyInputSheet.InputSheetId = "";
                ls.Insert(0, emptyInputSheet);
            }
            fromPurchaseView.SetInputSheetRecord(ls);
        }

        /// <summary>
        /// Load InputSheetDetails
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="inputSheetId"></param>
        private void LoadInputSheetDetails(string orderId, string inputSheetId)
        {
            List<InputSheetDetailRecord> ls = purchaseOrderService.GetInputSheetDetails(orderId, inputSheetId);
            fromPurchaseView.SetInputSheetDetailRecord(ls);
        }

        /// <summary>
        /// Save InputSheet
        /// </summary>
        /// <param name="vo"></param>
        private void SaveInputSheet(InputSheetParam vo)
        {
            purchaseOrderService.SaveInputSheet(vo);
        }

        private void DeleteInputSheet(string inputSheetId)
        {
            purchaseOrderService.DeleteInputSheet(inputSheetId);
        }

    }
}